﻿using System;
using System.Data;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using TrinityAPI.Base.Models;

namespace TrinityAPI.Data.MySql
{
    public class MySqlDBParameters
    {
        private DBParameter[] _parameters { get; set; }
        private List<DBParameter> _parameterList { get; set; }
        public MySqlDBParameters(List<DBParameter> parameterList)
        {
            this._parameters = null;
            this._parameterList = parameterList;
        }
        public MySqlDBParameters(DBParameter[] parameters)
        {
            this._parameters = parameters;
            this._parameterList = null;
        }

        public static implicit operator List<MySqlParameter>(MySqlDBParameters dbParameter)
        {
            List<MySqlParameter> paramList = new List<MySqlParameter>();
            if (dbParameter == null || (dbParameter._parameters == null && dbParameter._parameterList == null))
                return paramList;

            if (dbParameter._parameterList != null)
            {
                for (int i = 0; i < dbParameter._parameterList.Count; i++)
                {
                    var item = dbParameter._parameterList[i];
                    var param = new MySqlParameter(item.ParameterName, GetMYSQLDataType(item.DataType));
                    param.Direction = item.Direction;
                    param.IsNullable = item.IsNullable;
                    param.Precision = item.Precision;
                    param.Scale = item.Scale;
                    param.Size = item.Size;
                    param.SourceColumn = item.SourceColumn;
                    param.SourceVersion = item.SourceVersion;
                    param.Value = item.Value;
                    paramList.Add(param);
                }
            }
            else
            {
                for (int i = 0; i < dbParameter._parameters.Length; i++)
                {
                    var item = dbParameter._parameters[i];
                    var param = new MySqlParameter(item.ParameterName, GetMYSQLDataType(item.DataType));
                    param.Direction = item.Direction;
                    param.IsNullable = item.IsNullable;
                    param.Precision = item.Precision;
                    param.Scale = item.Scale;
                    param.Size = item.Size;
                    param.SourceColumn = item.SourceColumn;
                    param.SourceVersion = item.SourceVersion;
                    param.Value = item.Value;
                    paramList.Add(param);
                }
            }

            return paramList;
        }

        private static MySqlDbType GetMYSQLDataType(DbType type)
        {
            MySqlDbType dbType = MySqlDbType.String;

            switch (type)
            {
                case DbType.String:
                    dbType = MySqlDbType.String;
                    break;
                case DbType.Int32:
                    dbType = MySqlDbType.Int32;
                    break;
                case DbType.Int64:
                    dbType = MySqlDbType.Int64;
                    break;
                case DbType.Date:
                    dbType = MySqlDbType.Date;
                    break;
                case DbType.DateTime:
                    dbType = MySqlDbType.DateTime;
                    break;
                case DbType.Double:
                    dbType = MySqlDbType.Double;
                    break;
                case DbType.Decimal:
                    dbType = MySqlDbType.Decimal;
                    break;
                case DbType.Boolean:
                    dbType = MySqlDbType.Bit;
                    break;
                case DbType.Guid:
                    dbType = MySqlDbType.Guid;
                    break;
            }
            return dbType;
        }
    }
}
